/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import model.pojo.*;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author CUCAINHO
 */
public class KhachHangDAO {

    public static boolean KhachHangDangKy(Khachhang kh) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.save(kh);
            session.getTransaction().commit();


        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);
            return false;
        } finally {
            session.close();
        }
        return true;
    }

    public static List<Khachhang> LayDanhSachKhachHang() {

        List<Khachhang> danhSachKhachHang = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kh from Khachhang kh";
            Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
            danhSachKhachHang = query.list();
        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return danhSachKhachHang;
    }

    public static Khachhang KhachHangDangNhap(String username, String password) {

        Khachhang khachHang = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = " from Khachhang kh where kh.id.username=:username and kh.password=:password";

            Query query = session.createQuery(hql);
            query.setString("username", username);
            query.setString("password", password);

            //Query query = session.createQuery(hql);
            //Query query = session.createQuery(hql);             
            List<Khachhang> danhSachKhachHang = query.list();
            if (danhSachKhachHang.size() == 0) {
                return null;
            } else {
                khachHang = danhSachKhachHang.get(0);
            }

        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return khachHang;
    }
    
    public static boolean CheckKhachHang(String username) {

       
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = " from Khachhang kh where kh.id.username=:username";

            Query query = session.createQuery(hql);
            query.setString("username", username);           
       
            List<Khachhang> danhSachKhachHang = query.list();
            if (danhSachKhachHang.size() == 0) {
                return false;
            } else {
              return true;
            }

        } catch (HibernateException ex) { //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return false;
    }
    
    
}
